from django.conf import settings
from django.db import models
from django.utils.timezone import now
# Create your models here.


gender = (('1', '男'), ('2', '女'))


class Professional(models.Model):
    """系别模型"""
    name = models.CharField(max_length=50, blank=False, unique=True, verbose_name='专业名称')

    class Meta:
        db_table = 'Professional'
        verbose_name = '专业'
        verbose_name_plural = '专业'

    def __str__(self):
        return self.name


class University(models.Model):
    """高校模型"""
    name = models.CharField(max_length=50, blank=False, unique=True, verbose_name='高校名称')
    professionals = models.ManyToManyField(Professional, related_name="professional", verbose_name='专业集合')

    class Meta:
        db_table = 'University'
        verbose_name = '高校'
        verbose_name_plural = '高校'

    def __str__(self):
        return self.name


class TagToUser(models.Model):
    """用户使用的个性标签"""
    text = models.CharField(max_length=30, blank=False, unique=True, verbose_name='标签')

    class Meta:
        db_table = 'TagToUser'
        verbose_name = '用户个性标签'
        verbose_name_plural = '用户个性标签'

    def __str__(self):
        return self.text


class WeChatUser(models.Model):
    """微信用户模型"""
    openID = models.CharField(max_length=50, blank=False, unique=True, verbose_name='用户标识符')
    nickName = models.CharField(max_length=30, blank=False, verbose_name='昵称')
    gender = models.CharField(max_length=5, blank=False, choices=gender, default=gender[0], verbose_name='性别')
    avatarUrl = models.URLField(blank=False, verbose_name='头像地址')
    declaration = models.CharField(max_length=100,default="该用户没啥想说的", verbose_name='个性宣言')
    university = models.ForeignKey(University, blank=False, on_delete=models.CASCADE, related_name='user', verbose_name='关联学校')
    professional = models.ForeignKey(Professional, blank=False, on_delete=models.CASCADE, related_name='user', verbose_name='关联专业')
    tag = models.ManyToManyField(TagToUser, blank=True, verbose_name="个性标签")
    wanna = models.CharField(max_length=10,default='该用户不知道在想什么',blank=False,verbose_name="最近所想")
    created_time = models.DateTimeField(blank=False, default=now, verbose_name='注册时间')

    class Meta:
        db_table = 'WeChatUser'
        verbose_name = '用户'
        verbose_name_plural = '用户'
        ordering = ('-created_time',)

    def __str__(self):
        return self.nickName


class TagToQuestion(models.Model):
    """问题标签"""
    text = models.CharField(max_length=100, blank=False, unique=True, verbose_name='标签')

    class Meta:
        db_table = 'TagToQuestion'
        verbose_name = '问题标签'
        verbose_name_plural = '问题标签'

    def __str__(self):
        return self.text


class Question(models.Model):
    """问题模型"""
    title = models.CharField(max_length=100, blank=False, verbose_name='标题')
    describe = models.TextField(blank=False, verbose_name='描述')
    we_chat_user = models.ForeignKey(WeChatUser, blank=False, on_delete=models.CASCADE, verbose_name='提问者')
    solve = models.BooleanField(default=False, verbose_name='是否解决')
    tags = models.ManyToManyField(TagToQuestion, blank=False, verbose_name="问题标签")
    created_time = models.DateTimeField(default=now, blank=False, verbose_name='发表时间')

    class Meta:
        db_table = 'Question'
        verbose_name = '问题'
        verbose_name_plural = '问题'
        ordering = ('-created_time',)

    def __str__(self):
        return self.title


class ImageForQuestion(models.Model):
    """作为问题的关联附件"""
    question = models.ForeignKey(Question,blank=False,on_delete=models.CASCADE,verbose_name='关联问题')
    image = models.ImageField(upload_to="question/%Y/%m/%d", verbose_name='图例')

    class Meta:
        db_table = "ImageForQuestion"
        verbose_name = "问题附件"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.image)


class Answer(models.Model):
    """答复模型"""
    content = models.TextField(blank=False, verbose_name="内容")
    question = models.ForeignKey(Question, blank=False, on_delete=models.CASCADE, verbose_name="关联问题")
    we_chat_user = models.ForeignKey(WeChatUser, blank=False, on_delete=models.CASCADE, verbose_name="关联用户")
    optimum = models.BooleanField(default=False,blank=False,verbose_name="最佳答复")
    created_time = models.DateTimeField(default=now, verbose_name='上传时间')
    parent = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='children'
    )
    grandpa = models.ForeignKey(
        'self',
        null=True,
        blank=True,
        on_delete=models.SET_NULL
    )

    class Meta:
        db_table = "Answer"
        verbose_name = "答复"
        verbose_name_plural = verbose_name
        ordering = ("-created_time",)

    def __str__(self):
        return self.content


class ImageForAnswer(models.Model):
    """作为答复的关联附件"""
    answer = models.ForeignKey(Answer, blank=False, on_delete=models.CASCADE, verbose_name='关联答复')
    image = models.ImageField(upload_to="answer/%Y/%m/%d", verbose_name='图例')

    class Meta:
        db_table = "ImageForAnswer"
        verbose_name = "答复附件"
        verbose_name_plural = verbose_name

    def __str__(self):
        return str(self.image)


class Slideshow(models.Model):
    """轮播图模型"""
    cover = models.ImageField(upload_to='cover/%Y/%m/%d',verbose_name='标题图')
    title = models.CharField(max_length=30,blank=False,verbose_name='标题')
    content = models.TextField(verbose_name='内容')
    created_time = models.DateTimeField(default=now,verbose_name='创建时间')

    class Meta:
        db_table = "Slideshow"
        verbose_name = "轮播图"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class Feedback(models.Model):
    """反馈模型"""

    content = models.TextField(blank=False,verbose_name='反馈内容')
    user = models.ForeignKey(WeChatUser,blank=False,on_delete=models.CASCADE,verbose_name='反馈用户')
    created_time = models.DateTimeField(default=now,verbose_name='创建时间')

    class Meta:
        db_table = "Feedback"
        verbose_name = "反馈信息"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "反馈者" + self.user.nickName


class InviteRelationship(models.Model):
    """邀请关系模型"""
    sponsor = models.ForeignKey(WeChatUser,blank=False,on_delete=models.CASCADE,related_name='user_sponsor',verbose_name='主办者')
    invitee = models.ForeignKey(WeChatUser,blank=False,on_delete=models.CASCADE,related_name='user_invitee',verbose_name='受邀者')
    question = models.ForeignKey(Question,blank=False,on_delete=models.CASCADE,verbose_name='关联问题')
    accpet = models.BooleanField(default=False, verbose_name='接受状态')
    created_time = models.DateTimeField(default=now,verbose_name='创建时间')

    class Meta:
        db_table = "InviteRelationship"
        verbose_name = "邀请关系"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '邀请者:{}; 受邀者:{}; 关联问题:{}'.format(self.sponsor,self.invitee,self.question)


class ReplyRelationship(models.Model):
    """回复关系模型"""
    replier = models.ForeignKey(WeChatUser,blank=False,on_delete=models.CASCADE,related_name='user_replier',verbose_name='回答者')
    answer = models.ForeignKey(Answer,blank=False,on_delete=models.CASCADE,verbose_name='关联回复')
    recipient = models.ForeignKey(WeChatUser,blank=False,on_delete=models.CASCADE,related_name='user_recipient',verbose_name='接受方')
    question = models.ForeignKey(Question,blank=False,on_delete=models.CASCADE,verbose_name='关联问题')
    accpet = models.BooleanField(default=False,verbose_name='接受状态')
    created_time = models.DateTimeField(default=now,verbose_name='创建时间')

    class Meta:
        db_table = "ReplyRelationship"
        verbose_name = "回复关系"
        verbose_name_plural = verbose_name

    def __str__(self):
        return '回复者:{}; 被回复者:{}; 回复内容:{}'.format(self.replier,self.recipient,self.answer)


class BindUserAndQuestionTag(models.Model):
    """建立用户与哪些标签问题的关系"""

    user = models.ForeignKey(WeChatUser,blank=False,on_delete=models.CASCADE, verbose_name='用户')
    tag = models.ForeignKey(TagToQuestion,blank=False,on_delete=models.CASCADE,verbose_name='标签')

    class Meta:
        db_table = "BindUserAndQuestionTag"
        verbose_name = "绑定用户与问题标签"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.user.nickName
